package icu.javayanglei.bilibili.util;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.HexUtil;

import java.nio.charset.StandardCharsets;
import java.util.zip.CRC32;

/**
 * CRC-32 工具类
 *
 * @author yang
 * @version 1.0.0
 * @date 2022-01-14 20:21
 */
public class CRCUtil {
    /**
     * uid to mid
     * 用户 id 转 弹幕 id 加密
     *
     * @param uid 52704344
     * @return mid ec578ae5
     */
    public static String uidToMid(String uid) {
        CRC32 crc32 = new CRC32();
        crc32.update(uid.getBytes(StandardCharsets.UTF_8));
        final long value = crc32.getValue();
        return HexUtil.toHex(value);
    }

    public static String uidToMid(Integer uid) {
        return uidToMid(Convert.toStr(uid));
    }

    public static String uidToMid(long uid) {
        return uidToMid(Convert.toStr(uid));
    }
}
